package com.mes.controller.warehouse;

import com.mes.entity.User;
import com.mes.service.warehouse.SxkXjListService;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("sxkXjList")
public class SxkXjListController {
    @Autowired
    private SxkXjListService sxkXjListService;
    //根据机台号查出对应信息
    @ResponseBody
    @RequestMapping(value="selectAllEqList",method= RequestMethod.POST)
    public Message selectAllEqList(HttpServletRequest request){
        Message msg = new Message();

        String code = request.getParameter("eqCode").toString();

        msg.setData("AllLists", sxkXjListService.selectSxListByCode(code));

        return msg;

    }

    //查询颜色
    @ResponseBody
    @RequestMapping(value="selectColorLists",method= RequestMethod.POST)
    public Message selectColorLists(HttpServletRequest request){
        Message msg = new Message();


        msg.setData("ColorLists", sxkXjListService.selectColor());

        return msg;

    }


    //保存纱线库下架信息
    @ResponseBody
    @RequestMapping(value="saveCkXiajiaLists",method= RequestMethod.POST)
    public Message saveCkXiajiaLists(HttpServletRequest request){
        Message msg = new Message();
        try {
            String eqCodes = request.getParameter("eqCodes"); //设备机台
            String wlIds = request.getParameter("wlIds");     //物料ID
            String ysIds = request.getParameter("ysIds");     //颜色ID
            String dwIds = request.getParameter("dwIds");     //单位ID
            String xjqtys = request.getParameter("xjqtys");   //下架数量


           // List<Map<String, Object>> warHoserList  = sxkXjListService.selectCkId(eqCodes);

            String HOUSEID = "71622626D896478F85671DECAFD41976";//warHoserList.get(0).get("CKID").toString();

            String [] wlId = wlIds.split(",");
            String [] ysId = ysIds.split(",");
            String [] dwId = dwIds.split(",");
            String [] xjqty = xjqtys.split(",");
            String opt_mode = "纱线库下架"; //出入库方式
            String status = "OK"; //状态

            int numbers = wlId.length;

            String batchID = "";

            User user = UserUtil.getLoginUser(request);

            String adorgId = user.getOrgId();         //登录人的ad_org_id
            String adclientId = user.getClientId();   //登录人的ad_client_id
            String userIdtime = user.getId();			//登录人的ad_user_id
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date time  =sdf.parse(sdf.format(new Date())); //获取当前系统时间

            List<Map<String, Object>> ListUser = sxkXjListService.getEquipmentputCreateby(userIdtime);

            String createBy = ListUser.get(0).get("CREATEDBY").toString();  //登录人的CreateBy

            String updatedBy = ListUser.get(0).get("UPDATEDBY").toString(); //登录人的updateBy

            for(int i = 0 ; i< numbers ; i++){
                String productId = wlId[i];
                String colorId = ysId[i];
                String uomId = dwId[i];
                String qty = xjqty[i];

                Double qtys = Double.parseDouble(qty);

              List<Map<String, Object>> storage  = sxkXjListService.selectStorageBatch(productId,colorId);

              batchID = storage.get(0).get("BATCHID").toString();

            sxkXjListService.saveStorageLists(batchID,HOUSEID,qtys,uomId,opt_mode,status,adorgId,adclientId,createBy,updatedBy,time);
            }


        }catch (Exception e){
            msg.setData("results","false");
            e.printStackTrace();
        }
        return msg;

    }
}
